package com.wjh.api.service.email;

import com.alibaba.fastjson.JSONObject;
import com.wjh.adapter.MessageAdapter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * @Author: 王金海
 * @Description: 处理发送邮件
 * @Date: Created by Administrator on 2018/6/11.
 * @Modified By:
 */
@Service
@Slf4j
public class MailService implements MessageAdapter {
    @Value("${msg.subject}")
    private String subject;
    @Value("${msg.text}")
    private String text;
    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender; // 自动注入的Bean

    @Override
    public void sendMsg(JSONObject body) {
        String mail = body.getString("mail");
        log.info(mail);
        if (StringUtils.isEmpty(mail)) {
            return;
        }
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 发送
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(mail);
        // 标题
        simpleMailMessage.setSubject(subject);
        // 内容
        simpleMailMessage.setText(text.replace("{}", mail));
        mailSender.send(simpleMailMessage);
    }

}


